import jieba
from wordcloud import WordCloud
from PIL import Image
import numpy as np

# 新闻报道文本
file = open('2014network_hot_word.txt', encoding='UTF-8')
text = file.read()
file.close()

# 使用jieba进行分词
# words = " ".join([word for word in jieba.lcut(text) if len(word) > 1])


# 读取图片作为词云形状
picture = Image.open("douyin.jpeg")
mk = np.array(picture)

# 生成WordCloud对象
w = WordCloud(font_path='simhei.ttf',  # 确保选择了一个支持中文的字体
                      mask= mk,
                      background_color='white',
                      width=800,
                      height=600).generate(text)

# 指定生成图的路径并保存词云图片
output_path = '2014cloud.png'
w.to_file(output_path)

